package xyz.robinbingo.leetcode.a20.b06.c06;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

public class Solution {
    public int longestConsecutive(int[] nums) {
        Set<Integer> set = Arrays.stream(nums).boxed().collect(Collectors.toSet());
        int max = 0;
        for (int value : nums) {
            int num = value;
            //n - 1 是否存在
            if (!set.contains(num - 1)) {
                int count = 0;
                while (set.contains(num)) {
                    num++;
                    count++;
                }
                max = Math.max(max, count);
            }
        }
        return max;
    }
}